package vip.idiy.anwei_android_iot.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

import vip.idiy.anwei_android_iot.R;
import vip.idiy.anwei_android_iot.enums.SceneIconEnum;
import vip.idiy.anwei_android_iot.enums.TypeImgEnum;

/**
 * ClassName IotUtil
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 18:57 2023/4/20
 */
public class IotUtil {
    private static final String TAG = "myTag " + " IotUtil ";

    /**
     * 判断图标
     *
     * @param iconName
     * @return
     */
    public static SceneIconEnum checkIcon(String iconName) {
        SceneIconEnum[] values = SceneIconEnum.values();
        for (SceneIconEnum c : values) {
            if (c.getIconName().equals(iconName)) {
                return c;
            }
        }
        return SceneIconEnum.guest;
    }

    /**
     * 判断图片
     *
     * @param imgName
     * @return
     */
    public static TypeImgEnum checkImg(String imgName) {
        TypeImgEnum[] values = TypeImgEnum.values();
        for (TypeImgEnum c : values) {
            if (c.getIconName().equals(imgName)) {
                return c;
            }
        }
        return TypeImgEnum.switch_lighting;
    }

    /**
     * 根据list.size()返回页码
     *
     * @param size
     * @return
     */
    public static int getTotalPages(int size) {
        int pageSize = 10; // 每页显示的记录数
        return size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
    }


    /**
     * @param size     第几页
     * @param pageSize 每页记录数
     * @return
     */
    public static int getTotalPages(int size, int pageSize) {
        return size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
    }

    /**
     * 获取当前wifi
     *
     * @param context
     * @return
     */
    public static String getConnectedWifiSsid(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String ssid = wifiInfo.getSSID();
            if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
                ssid = ssid.substring(1, ssid.length() - 1);
            }
            return ssid;
        }
        return null;
    }

    /**
     * 判断是否5G_WIFI
     * @param context
     * @return
     */
    public static boolean is5G(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int frequency = wifiInfo.getFrequency();
            return (frequency >= 4900 && frequency <= 5900);
        }
        return false;
    }

    /**
     * 获取bssid
     * @param context
     * @return
     */
    public static String getConnectedWifiBssid(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            return wifiInfo.getBSSID();
        }
        return null;
    }


    /**
     * 解析json
     *
     * @param jsonStr
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T analysisResult(String jsonStr, Class<T> type) {
        return JSON.parseObject(jsonStr, type);
    }

    /**
     * 解析JSON字符串中的列表
     *
     * @param objStr  JSON字符串
     * @param objName 列表名称
     * @param type    列表元素类型
     * @return 解析后的列表
     */
    public static <T> List<T> analysisList(String objStr, String objName, Class<T> type) {
        JSONObject jsonObject = (JSONObject) JSON.parse(objStr);
        Object o = jsonObject.get(objName);
        if (o == null)
            return null;
        return JSON.parseArray(o.toString(), type);
    }

    /**
     * 检查wifi信号强度
     *
     * @param rssi
     * @return
     */
    public static int checkRssi(int rssi) {
        if (rssi < 1 && rssi >= -55) {
            return R.mipmap.icon_wifi_4;
        } else if (rssi < -55 && rssi >= -70) {
            return R.mipmap.icon_wifi_3;
        } else if (rssi < -70 && rssi >= -85) {
            return R.mipmap.icon_wifi_2;
        } else if (rssi < -85 && rssi >= -100) {
            return R.mipmap.icon_wifi_1;
        }
        return R.mipmap.icon_wifi_0;
    }
}
